<template>
  <div v-if="open" class="modal-overlay" @click.self="$emit('close')">
    <div class="modal card">
      <header class="modal__header">
        <h3>注册企业账号</h3>
        <button class="modal__close" @click="$emit('close')">×</button>
      </header>
      <p class="modal__intro">
        输入手机号并设置密码，系统会发送短信验证码。注册后可进入会员中心完善企业信息。
      </p>
      <form class="modal__form" @submit.prevent="handleSubmit">
        <label>
          <span>手机号</span>
          <input v-model.trim="form.phone" type="tel" required maxlength="11" />
        </label>
        <div class="modal__row">
          <label>
            <span>验证码</span>
            <input v-model.trim="form.code" type="text" required maxlength="6" />
          </label>
          <button type="button" class="btn btn--ghost" :disabled="countdown > 0" @click="sendCode">
            {{ countdown > 0 ? `${countdown}s` : "发送验证码" }}
          </button>
        </div>
        <label>
          <span>密码</span>
          <input v-model="form.password" type="password" required minlength="6" />
        </label>
        <label>
          <span>昵称 / 企业简称</span>
          <input v-model.trim="form.nickname" type="text" required />
        </label>
        <button type="submit" class="btn btn--primary" :disabled="submitting">
          {{ submitting ? "注册中..." : "注册" }}
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onUnmounted } from "vue";
import { message } from "../utils/message";
import { register, sendSmsCode } from "../api/auth";

const props = defineProps({
  open: { type: Boolean, default: false }
});

const emit = defineEmits(["close", "registered"]);

const form = reactive({
  phone: "",
  code: "",
  password: "",
  nickname: ""
});

const submitting = ref(false);
const countdown = ref(0);
let timer = null;

const sendCode = async () => {
  if (!form.phone || form.phone.length !== 11) {
    message.warning("请输入有效的手机号");
    return;
  }
  try {
    await sendSmsCode({ phone: form.phone, scene: "register" });
    message.success("验证码已发送");
    countdown.value = 60;
    timer = window.setInterval(() => {
      countdown.value -= 1;
      if (countdown.value <= 0 && timer) {
        window.clearInterval(timer);
        timer = null;
      }
    }, 1000);
  } catch (error) {
    message.error(error.message || "发送验证码失败");
  }
};

const handleSubmit = async () => {
  submitting.value = true;
  try {
    const payload = {
      phone: form.phone,
      password: form.password,
      code: form.code,
      nickname: form.nickname
    };
    await register(payload);
    message.success("注册成功，请登录");
    emit("registered", payload.phone);
    emit("close");
    Object.assign(form, { phone: "", code: "", password: "", nickname: "" });
  } catch (error) {
    message.error(error.message || "注册失败");
  } finally {
    submitting.value = false;
  }
};

onUnmounted(() => {
  if (timer) {
    window.clearInterval(timer);
  }
});
</script>

<style scoped lang="scss">
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.45);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 2100;
}

.modal {
  max-width: 420px;
  width: 100%;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.modal__header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  h3 {
    margin: 0;
  }
}

.modal__close {
  border: none;
  background: none;
  font-size: 24px;
  color: var(--gray-500);
}

.modal__intro {
  margin: 0;
  color: var(--gray-600);
  font-size: 14px;
}

.modal__form {
  display: flex;
  flex-direction: column;
  gap: 16px;

  label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 14px;
    color: var(--gray-600);
  }

  input {
    padding: 10px 14px;
    border-radius: 10px;
    border: 1px solid var(--gray-200);

    &:focus {
      border-color: var(--primary-color);
      outline: none;
      box-shadow: 0 0 0 2px rgba(0, 82, 217, 0.18);
    }
  }
}

.modal__row {
  display: flex;
  gap: 12px;
  align-items: flex-end;

  label {
    flex: 1;
  }

  .btn {
    white-space: nowrap;
  }
}

.btn {
  border-radius: 10px;
  border: none;
  padding: 10px 20px;
  font-weight: 600;

  &--primary {
    background: var(--primary-color);
    color: #fff;
  }

  &--ghost {
    background: var(--gray-100);
    color: var(--primary-color);
  }
}
</style>
